package com.pactera.madp.cp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.dto.moments.MomentsPageDTO;
import com.pactera.madp.cp.api.dto.moments.MomentsTaskAddDTO;
import com.pactera.madp.cp.api.dto.moments.MomentsTaskUpdateDTO;
import com.pactera.madp.cp.api.vo.moments.MomentsDetailsVO;
import com.pactera.madp.cp.api.vo.moments.MomentsPageVO;
import com.pactera.madp.cp.api.vo.moments.MomentsTaskDetailsPageVO;
import com.pactera.madp.cp.api.vo.moments.MomentsTaskDetailsVO;
import com.pactera.madp.cp.service.CpMomentsTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * 朋友圈任务表(CpMomentsTask)表控制层
 *
 * @author jun.lei
 * @since 2022-05-13 10:15:47
 */
@RestController
@RequestMapping("/moments-task")
@Api(value = "moments-task", tags = "朋友圈任务管理")
@AllArgsConstructor
@Slf4j
public class CpMomentsTaskController {

    private CpMomentsTaskService cpMomentsTaskService;

    @PostMapping
    @ApiOperation("新增朋友圈任务")
    public R saveMomentsTask(@Validated @RequestBody MomentsTaskAddDTO dto) {
        return cpMomentsTaskService.saveMomentsTask(dto) ? R.ok() : R.failed();
    }


    @PutMapping
    @ApiOperation("修改朋友圈任务")
    public R updateMomentsTask(@Validated @RequestBody MomentsTaskUpdateDTO dto) {
        return cpMomentsTaskService.updateMomentsTask(dto) ? R.ok() : R.failed();
    }

    @GetMapping("/page")
    @ApiOperation("朋友圈任务分页")
    public R<Page<MomentsPageVO>> getMomentsPage(MomentsPageDTO dto, Page page) {
        return R.ok(cpMomentsTaskService.getMomentPage(dto, page));
    }

    @PutMapping("/release")
    @ApiOperation("发布朋友圈任务")
    public R releaseMomentsTask(@RequestParam("id") Integer id) {
        return cpMomentsTaskService.releaseMomentsTask(id) ? R.ok() : R.failed();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取朋友圈任务详情")
    public R<MomentsDetailsVO> getMomentsDetails(@PathVariable Integer id) {
        return R.ok(cpMomentsTaskService.getMomentsDetails(id));
    }

    @GetMapping("/revoke/{id}")
    @ApiOperation("朋友圈任务撤销")
    public R revoke(@PathVariable Integer id) {
        return cpMomentsTaskService.revoke(id) ? R.ok() : R.failed();
    }

    @PutMapping("/update-status")
    @ApiOperation("修改朋友圈任务状态")
    @Inner
    public void updateStatus() {
        cpMomentsTaskService.updateStatus();
    }


    @GetMapping("/task-details")
    @ApiOperation("朋友圈任务详情")
    public R<MomentsTaskDetailsVO> getMomentsDetailsAll(@RequestParam("id") Integer id){
      return R.ok(cpMomentsTaskService.getMomentsDetailsAll(id));
    }

    @PutMapping("/syn-task")
    @ApiOperation("定时同步朋友圈任务详情")
    @Inner
    public void synMomentsTaskDetails(){
        RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(),true);
        cpMomentsTaskService.synMomentsTaskDetails();
    }

    @GetMapping("/page-details")
    @ApiOperation("朋友圈任务详情-分页")
    public R<Page<MomentsTaskDetailsPageVO>> getMomentTaskDetailsPage(@RequestParam("id") Integer id,Page page,String name){
        return R.ok(cpMomentsTaskService.getMomentTaskDetailsPage(id, page,name));
    }

    @DeleteMapping("/overdue-tasks")
    @ApiOperation("删除过期30天的任务")
    @Inner
    public void deleteOverdueTasks(){
        cpMomentsTaskService.deleteOverdueTasks();
    }

}

